<?php

class Jaco extends FrontEnd_Controller {

    function Jaco() {
        parent::FrontEnd_Controller();
        $this->_data['erreur'] = "";
    }

    //*********Language**********************

    function changelangue($key) {
        $this->session->set_userdata('langue', $key);
    }

    //*********Page**********************


    function index() {
        $this->translate();
        $this->session->set_userdata('url_toreturn', 'jaco');

        $key = $this->session->userdata('langue');
        $listproduct = $this->home_m->get_product(5, 0);
        $listactu = $this->home_m->get_actu(5, 0);
        $content = $this->home_m->get_content($key);

        $this->_data['content'] = $content['content'];
        $this->_data['listactu'] = $listactu;
        $this->_data['listproduct'] = $listproduct;
        $this->_data['is_banner'] = true;
        $this->_data['is_sidebar'] = false;


        $this->display_view('home');
    }

    function notre_entreprise() {
        $this->translate();
        $this->session->set_userdata('url_toreturn', 'jaco/notre_entreprise');

        $sidebar['1'] = "modules/categories";

        $key = $this->session->userdata('langue');
        $listcat = $this->home_m->get_all_categories();
        $content = $this->home_m->get_content($key);

        $this->_data['content'] = $content['content'];

        $this->_data['listcat'] = $listcat;
        $this->_data['listsidebar'] = $sidebar;
        $this->display_view('entreprise');
    }

    function information($page) {
        $this->translate();
        $this->session->set_userdata('url_toreturn', 'jaco/information/' . $page);

        $sidebar['1'] = "modules/categories";

        $key = $this->session->userdata('langue');
        $listcat = $this->home_m->get_all_categories();
        $information = $this->home_m->get_information($key, $page);
        $this->_data['information'] = $information[$key];

        $this->_data['listcat'] = $listcat;
        $this->_data['listsidebar'] = $sidebar;
        $this->display_view('information');
    }

    function nos_animaux($id=0) {
        $this->session->set_userdata('url_toreturn', 'jaco/nos_animaux/' . $id);
        $catid = (int) $id;
        $sidebar['1'] = "modules/categories";
        $sidebar['2'] = "modules/listactu";
        $this->translate();

        $per_page = $this->uri->segment(4);
        $offset = $this->uri->segment(5);
        if ($per_page == ""
            )$per_page = 16;
        if ($offset == "")
            $offset = 0;
        $config['base_url'] = base_url() . 'index.php/jaco/nos_animaux/' . $catid . '/' . $per_page . '/';
        $config['per_page'] = '16';
        $config['uri_segment'] = 5;
        $config['full_tag_open'] = "<div class='contain_page'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="current_page"> [';
        $config['cur_tag_close'] = '] </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';
        $listcat = $this->home_m->get_all_categories();
        $listcatchild = $this->home_m->get_all_id_categories_child($catid);
        $listactuleft = $this->home_m->get_actu(5, 0);
        if ($catid == 0) {
            $catimg['img'] = "";
            $config['total_rows'] = $this->db->count_all('animaux');
            $listproduct = $this->home_m->get_product($config['per_page'], $offset);
            $cat_link = $this->_data['langue']['nav_nos_animaux'];
        } else {
            $catimg = $this->home_m->get_img_categories($catid);
            $config['total_rows'] = $this->home_m->count_product_categories($listcatchild['id']);
            $listproduct = $this->home_m->get_product_categories($listcatchild['id'], $config['per_page'], $offset);
            $key = $this->session->userdata('langue');
            $cat_link = "<a href='" . site_url('jaco/nos_animaux') . "' >" . $this->_data['langue']['nav_nos_animaux'] . '</a>';
            $cat_link.=' > ' . $catimg['nom' . $key];
        }

        $this->pagination->initialize($config);
        $this->_data['catimg'] = $catimg['img'];
        $this->_data['cat_link'] = $cat_link;
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['listcatchild'] = $listcatchild['array'];
        $this->_data['listactuleft'] = $listactuleft;
        $this->_data['listsidebar'] = $sidebar;

        $this->_data['listproduct'] = $listproduct;
        $this->_data['listcat'] = $listcat;

        $this->display_view('animaux');
    }

    function disponible($id=0) {
        $this->session->set_userdata('url_toreturn', 'jaco/disponible/' . $id);
        $catid = (int) $id;
        $sidebar['1'] = "modules/categories";
        $sidebar['2'] = "modules/listactu";
        $this->translate();

        $per_page = $this->uri->segment(4);
        $offset = $this->uri->segment(5);
        if ($per_page == ""
            )$per_page = 16;
        if ($offset == "")
            $offset = 0;
        $config['base_url'] = base_url() . 'index.php/jaco/disponible/' . $catid . '/' . $per_page . '/';
        $config['per_page'] = '16';
        $config['uri_segment'] = 5;
        $config['full_tag_open'] = "<div class='contain_page'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="current_page">[ ';
        $config['cur_tag_close'] = '] </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';

        $listcat = $this->home_m->get_all_categories();
        $listcatchild = $this->home_m->get_all_id_categories_child($catid);
        $listactuleft = $this->home_m->get_actu(5, 0);
        if ($catid == 0) {
            $catimg['img'] = "";
            $config['total_rows'] = $this->home_m->count_product('1');
            $listproduct = $this->home_m->get_product($config['per_page'], $offset, '1');
            $cat_link = $this->_data['langue']['nav_nos_animaux'];
        } else {
            $catimg = $this->home_m->get_img_categories($catid);
            $config['total_rows'] = $this->home_m->count_product_categories($listcatchild['id'], 1);
            $listproduct = $this->home_m->get_product_categories($listcatchild['id'], $config['per_page'], $offset, 1);
            $key = $this->session->userdata('langue');
            $cat_link = "<a href='" . site_url('jaco/nos_animaux') . "' >" . $this->_data['langue']['nav_nos_animaux'] . '</a>';
            $cat_link.=' > ' . $catimg['nom' . $key];
        }
        $this->_data['catimg'] = $catimg['img'];
        $this->_data['cat_link'] = $cat_link;
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['listcatchild'] = $listcatchild['array'];
        $this->_data['listactuleft'] = $listactuleft;
        $this->_data['listsidebar'] = $sidebar;

        $this->_data['listproduct'] = $listproduct;
        $this->_data['listcat'] = $listcat;

        $this->display_view('dispo');
    }

    function recherche($id=0) {
        $recherche = $this->input->post('search');
        $catid = (int) $id;
        $per_page = $this->uri->segment(4);
        $offset = $this->uri->segment(5);
        if ($per_page == ""
            )$per_page = 16;
        if ($offset == "")
            $offset = 0;
        redirect(base_url() . 'index.php/jaco/recherche_page/' . $recherche . '/' . $catid . '/' . $per_page . '/');
    }

    function recherche_page($recherche, $id=0) {
        $this->session->set_userdata('url_toreturn', 'jaco/recherche/' . $id);

        $catid = (int) $id;
        $sidebar['1'] = "modules/recherche_categories";
        $sidebar['2'] = "modules/listactu";
        $this->translate();
        $per_page = $this->uri->segment(5);
        $offset = $this->uri->segment(6);
        if ($per_page == ""
            )$per_page = 16;
        if ($offset == "")
            $offset = 0;
        $config['base_url'] = base_url() . 'index.php/jaco/recherche_page/' . $recherche . '/' . $catid . '/' . $per_page . '/';
        $config['per_page'] = '16';
        $config['uri_segment'] = 6;
        $config['full_tag_open'] = "<div class='contain_page'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="current_page"> [';
        $config['cur_tag_close'] = '] </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';
        $listcat = $this->home_m->get_all_categories();
        $listcatchild = $this->home_m->get_all_id_categories_child($catid);
        $listactuleft = $this->home_m->get_actu(5, 0);
        if ($catid == 0) {
            $catimg['img'] = "";
            $config['total_rows'] = $this->home_m->count_product_recherche($recherche);
            $listproduct = $this->home_m->get_product_recherche($recherche, $config['per_page'], $offset);
        } else {
            $catimg = $this->home_m->get_img_categories($catid);
            $config['total_rows'] = $this->home_m->count_product_categories_recherche($recherche, $listcatchild['id']);
            $listproduct = $this->home_m->get_product_categories_recherche($recherche, $listcatchild['id'], $config['per_page'], $offset);
        }

        $this->pagination->initialize($config);
        $this->_data['catimg'] = $catimg['img'];
        $this->_data['recherche'] = $recherche;
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['listcatchild'] = $listcatchild['array'];
        $this->_data['listactuleft'] = $listactuleft;
        $this->_data['listsidebar'] = $sidebar;

        $this->_data['listproduct'] = $listproduct;
        $this->_data['listcat'] = $listcat;

        $this->display_view('recherche');
    }

    function detail($id=0, $name='') {
        $this->session->set_userdata('url_toreturn', 'jaco/detail/' . $id);
        $id = (int) $id;
        $sidebar['1'] = "modules/categories";
        $this->translate();
        $listcat = $this->home_m->get_all_categories();
        $detailproduct = $this->home_m->get_detail_product($id);


        $this->_data['listsidebar'] = $sidebar;
        $this->_data['listcat'] = $listcat;
        $this->_data['detailproduct'] = $detailproduct;
        $this->display_view('productdetail');
    }

    function actualites($id=0) {
        $this->session->set_userdata('url_toreturn', 'jaco/actualites/' . $id);
        $catid = (int) $id;
        $sidebar['1'] = "modules/catactu";
        $this->translate();
        $per_page = $this->uri->segment(4);
        $offset = $this->uri->segment(5);
        if ($per_page == ""
            )$per_page = 16;
        if ($offset == "")
            $offset = 0;
        $config['base_url'] = base_url() . 'index.php/jaco/actualites/' . $catid . '/' . $per_page . '/';
        $config['per_page'] = '16';
        $config['uri_segment'] = 5;
        $config['full_tag_open'] = "<div class='contain_page'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="current_page"> [';
        $config['cur_tag_close'] = '] </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';

        $listcat = $this->home_m->get_all_categories_actu();

        if ($catid == 0) {
            $config['total_rows'] = $this->db->count_all('actu');
            $listactu = $this->home_m->get_actu($config['per_page'], $offset);
        } else {
            $config['total_rows'] = $this->home_m->count_actu_categories($catid);
            $listactu = $this->home_m->get_actu_categories($catid, $config['per_page'], $offset);
        }

        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();

        $this->_data['listcat'] = $listcat;
        $this->_data['listsidebar'] = $sidebar;
        $this->_data['listactu'] = $listactu;

        $this->display_view('actu');
    }

    function detail_actualites($id=0, $name='') {
        $this->session->set_userdata('url_toreturn', 'jaco/detail_actualites/' . $id);
        $id = (int) $id;
        $sidebar['1'] = "modules/catactu";
        $this->translate();
        $listcat = $this->home_m->get_all_categories_actu();
        $detailactu = $this->home_m->get_detail_actu($id);
        $this->_data['listcat'] = $listcat;

        $this->_data['listsidebar'] = $sidebar;
        $this->_data['detailactu'] = $detailactu;
        $this->display_view('detail_actu');
    }

    function contact($action='') {
        $this->session->set_userdata('url_toreturn', 'jaco/contact');
        $this->translate();
        $config = array(
            array('field' => 'prenom',
                'rules' => 'required|xss_clean'
            ),
            array('field' => 'nom',
                'rules' => 'required|xss_clean'
            ),
            array('field' => 'email',
                'rules' => 'required|valid_email'
            ),
            array('field' => 'telephone',
                'rules' => 'required|xss_clean'
            ),
            array('field' => 'sujet',
                'rules' => 'required|xss_clean'
            ),
            array('field' => 'contenu',
                'rules' => 'required|xss_clean'
            )
        );
        $this->form_validation->set_rules($config);

        $sidebar['1'] = "modules/adresse";
        $this->_data['listsidebar'] = $sidebar;


        if ($action == 'envoyer') {
            if ($this->form_validation->run() == TRUE) {
                $dat = date('Y-m-d');
                $data = array(
                    'nom' => $this->input->post('nom'),
                    'prenom' => $this->input->post('prenom'),
                    'email' => $this->input->post('email'),
                    'tel' => $this->input->post('telephone'),
                    'sujet' => $this->input->post('sujet'),
                    'dat' => $dat,
                    'contenu' => $this->input->post('contenu')
                );
                $query = $this->home_m->envoyer($data);
                if ($query == 1)
                    $this->display_view('contact_reussi');
                else
                    $this->display_view('contact');
            } else
                $this->display_view('contact');
        } else
            $this->display_view('contact');
    }

    //***************CONNEXION, DECONNEXION, INSCRIPTION

    function connexion($action="") {
        $this->translate();
        $sidebar['1'] = "modules/adresse";
        $this->_data['listsidebar'] = $sidebar;


        if ($action == 'envoyer') {
            $config = array(
                array('field' => 'email',
                    'rules' => 'required|valid_email'
                ),
                array('field' => 'pass',
                    'rules' => 'required|md5'
                )
            );
            $this->form_validation->set_rules($config);

            if ($this->form_validation->run() == TRUE) {
                $data = array(
                    'email' => $this->input->post('email'),
                    'pass' => $this->input->post('pass')
                );

                $query = $this->home_m->check_connexion($data);


                if ($query->num_rows() > 0) {
                    $row = $query->row();
                    $this->session->set_userdata('clientid', $row->id);

                    $this->session->set_userdata('clientname', $row->nom);
                    $url_toreturn = $this->session->userdata('url_toreturn');
                    if ($url_toreturn != null && $url_toreturn != "")
                        redirect(site_url($url_toreturn));
                    else
                        redirect(site_url('jaco'));
                }else {
                    $this->_data['erreur'] = "Identifiant ou mot de passe n'existe pas";
                    $this->display_view('connexion');
                }
            } else
                $this->display_view('connexion');
        } else
            $this->display_view('connexion');
    }

    function deconnexion() {
        $array_items = array('clientname' => '', 'clientid' => '');
        $this->session->unset_userdata($array_items);
        redirect(base_url() . 'index.php/jaco');
    }

    function inscription($action="") {
        $this->translate();
        $config = array(
            array('field' => 'prenom',
                'rules' => 'required|min_length[3]|max_length[12]|xss_clean'
            ),
            array('field' => 'nom',
                'rules' => 'required|min_length[3]|max_length[12]|xss_clean'
            ),
            array('field' => 'email',
                'rules' => 'required|valid_email'
            ),
            array('field' => 'telephone',
                'rules' => 'required|xss_clean'
            ),
            array('field' => 'adresse',
                'rules' => 'required|xss_clean'
            ),
            array('field' => 'confirm_pass',
                'rules' => 'required|md5'
            ),
            array('field' => 'pass',
                'rules' => 'required|matches[confirm_pass]|md5'
            )
        );
        $this->form_validation->set_rules($config);
        $sidebar['1'] = "modules/adresse";
        $this->_data['listsidebar'] = $sidebar;


        if ($action == 'envoyer') {
            if ($this->form_validation->run() == TRUE) {

                $dat = date('Y-m-d');
                $data = array(
                    'nom' => $this->input->post('nom'),
                    'prenom' => $this->input->post('prenom'),
                    'email' => $this->input->post('email'),
                    'tel' => $this->input->post('telephone'),
                    'adresse' => $this->input->post('adresse'),
                    'dat' => $dat,
                    'pass' => $this->input->post('pass')
                );

                $query = $this->home_m->check_inscription($data);
                if ($query->num_rows() == 0) {

                    $query = $this->home_m->envoyer_inscription($data);
                    if ($query == 1)
                        $this->display_view('inscription_reussi');
                    else {
                        $this->_data['erreur'] = "Erreur lors de l'inscription !";
                        $this->display_view('inscription');
                    }
                } else {
                    $this->_data['erreur'] = "Email existe !";
                    $this->display_view('inscription');
                }
            } else
                $this->display_view('inscription');
        } else
            $this->display_view('inscription');
    }

    //************PANIER********************************
    function ajout_panier($id, $qty, $price, $name, $img) {

        $data = array(
            'id' => $id,
            'qty' => $qty,
            'price' => $price,
            'name' => $name,
            'img' => $img
        );

        $this->cart->insert($data);
        echo $this->cart->total() . " &euro; ";
    }

    function panier() {
        $this->translate();
        $this->session->set_userdata('url_toreturn', 'jaco/panier');
        $sidebar['1'] = "modules/categories";

        $listcat = $this->home_m->get_all_categories();
        $list_panier = $this->cart->contents();
        $this->_data['list_panier'] = $list_panier;

        $this->_data['listsidebar'] = $sidebar;
        $this->_data['listcat'] = $listcat;
        $this->display_view('panier');
    }

    function acheter_etape1($action="") {
        $this->translate();
        $config = array(
            array('field' => 'adr',
                'rules' => 'required|min_length[3]|xss_clean'
            ),
            array('field' => 'promo',
                'rules' => 'xss_clean'
            )
        );
        $this->form_validation->set_rules($config);
        if ($action == 'verifier') {
            if ($this->form_validation->run() == TRUE) {
                if($this->input->post('promo')!=""){
                $query = $this->home_m->check_promo($this->input->post('promo'));
               if ($query->num_rows() >0) {
                  $this->session->set_userdata('promo',$this->input->post('promo'));
                  $this->session->set_userdata('adr', $this->input->post('adr'));
                $this->session->set_userdata('livraison', $this->input->post('livraison'));
                redirect(site_url('jaco/acheter_etape2'));
               } else  $this->_data['erreur'] = "Code de promo invalide";
                } else {
                  $this->session->set_userdata('promo','');
                 $this->session->set_userdata('adr', $this->input->post('adr'));
                $this->session->set_userdata('livraison', $this->input->post('livraison'));
                redirect(site_url('jaco/acheter_etape2'));    
                }

            }
        }
        $sidebar['1'] = "modules/categories";
        $nav_link = $this->_data['langue']['top_nav'];
        $idclient = $this->session->userdata('clientid');
        $livraison = $this->home_m->get_livraison();
        $listcat = $this->home_m->get_all_categories();
        $client = $this->home_m->get_client($idclient);

        $this->_data['client'] = $client;

        $this->_data['livraison'] = $livraison;
        $this->_data['listsidebar'] = $sidebar;
        $this->_data['listcat'] = $listcat;

        if ($idclient == NULL)
            $this->display_view('demande_connexion');
        else
            $this->display_view('etap1');
    }

    function acheter_etape2($action="") {
        $this->translate();
        $sidebar['1'] = "modules/categories";

        $idclient = $this->session->userdata('clientid');
        $listcat = $this->home_m->get_all_categories();
        $client = $this->home_m->get_client($idclient);
        $method = $this->home_m->get_method();

        $this->_data['method'] = $method;
        $this->_data['client'] = $client;

        $this->_data['listsidebar'] = $sidebar;
        $this->_data['listcat'] = $listcat;
        if ($idclient == NULL)
            $this->display_view('demande_connexion');
        else
            $this->display_view('etap2');
    }

    function supprimer_tous_panier() {
        $this->cart->destroy();
    }

    function supprimer_tous_panier_redirect() {
        $this->cart->destroy();
        redirect(site_url('jaco/panier'));
    }

    function supprimer_produit_panier($rowid) {
        $data = array(
            'rowid' => $rowid,
            'qty' => 0
        );
        $this->cart->update($data);
        redirect(site_url('jaco/panier'));
    }

}

?>